package Practice0915;

import Homework.Commodity;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FilPress {
    private static List<Commodity> list = new ArrayList<>();
    static {
        Reader r = null;
        BufferedReader br = null;
        try {
            r = new FileReader("Housework09133/Product.txt");
            br = new BufferedReader(r);

            String str = null;
            while ((str = br.readLine())!=null){
                String[] array = str.split("\\s+");
                Commodity c = new Commodity(Integer.parseInt(array[0]),array[1],array[2],array[3],array[4]);

                list.add(c);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
                r.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writer(){
        Writer w = null;
        try {
            w = new FileWriter("Housework09133/Product.txt");
            for (Commodity c:list){
                w.write(c.getCode()+"  "+c.getName()+"  "+c.getPrice()+"  "+c.getFactory()+"  "+c.getDate()
                        +"\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                w.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static List<Commodity> getList() {
        return list;
    }
}
